package com.xiaofeng.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.xiaofeng.common.Const;
import com.xiaofeng.pojo.User;
import com.xiaofeng.service.iservice.Userservice;

@Controller("userAction")
public class UserAction implements ModelDriven<User>,SessionAware,CookiesAware,ServletRequestAware,ServletResponseAware{
	User user = new User();
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	private String autoLogin;
	private String keepPassword;
	
	public String getAutoLogin() {
		return autoLogin;
	}


	public void setAutoLogin(String autoLogin) {
		this.autoLogin = autoLogin;
	}


	public String getKeepPassword() {
		return keepPassword;
	}


	public void setKeepPassword(String keepPassword) {
		this.keepPassword = keepPassword;
	}
	private HttpServletResponse response;
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}
	
	private HttpServletRequest request;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	// 定义sessionMap
	
	private Map<String, Object> sessionMap;
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;	
	}
	// 定义cookieMap
	private Map<String,String> cookieMap;
	@Override
	public void setCookiesMap(Map<String, String> cookieMap) {
		this.cookieMap = cookieMap;
		
	}
	@Autowired
	private Userservice userService;
	
	// 取回valiCode
	private String valiCode;
	public String getValiCode() {
		return valiCode;
	}
	public void setValiCode(String valiCode) {
		this.valiCode = valiCode;
	}
	// 从session 调取 valiCode
	/*HttpServletRequest request1 = ServletActionContext.getRequest();
	HttpSession session = request1.getSession();
	String code = session.getAttribute("code").toString();*/
   //String code = request.getAttribute("code").toString();
	//String code = sessionMap.get("code").toString();
	/*HttpServletRequest request1 = ServletActionContext.getRequest();
	HttpSession session = request1.getSession();*/

	public String registry(){
		String code = sessionMap.get("code").toString();
		String msg = Const.ERROR;
		System.out.println(code);
		System.out.println(valiCode);
		if(code.equalsIgnoreCase(valiCode) ){
			msg = userService.registry(user);
		}
		return msg;	
	}

		
	


}
